package com.t235.gc.gateway.Goods;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.t235.gc.AjaxResult;
import com.t235.gc.entity.Goods;
import com.t235.gc.entity.GoodsCategory;
import com.t235.gc.gateway.RestTempUtils;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping
@CrossOrigin
public class GoodsController {
    @Resource
    private RestTempUtils restTempUtils;
    @GetMapping("/api/goods-category/admin/getAllGoodsCategoryList")
    public Object getAllGoodsCategoryList(HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        //System.out.println(requestURI);
        String url = "http://localhost:8003" + requestURI;
        System.out.println(url);
        return restTempUtils.getForObject(url, AjaxResult.class);
    }
    @GetMapping("/api/goods/admin/getBackGoodsList")
    public Object getBackGoodsList(HttpServletRequest request,Goods goods, Integer page, Integer limit) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("goodsid",goods.getGoodsid());
        paramMap.put("goodsname",goods.getGoodsname());
        paramMap.put("cname",goods.getCname());
        paramMap.put("goodscategoryid",goods.getGoodscategoryid());
        paramMap.put("page",page);
        paramMap.put("limit",limit);
        String urlByCs="?goodsid={goodsid}&goodsname={goodsname}&cname={cname}&goodscategoryid={goodscategoryid}" +
                "&page={page}&limit={limit}";
        String requestURI = request.getRequestURI();
        //System.out.println(requestURI);
        String url = "http://localhost:8003" + requestURI+urlByCs;
        System.out.println(url);
        return restTempUtils.getForObjectByAll(url, AjaxResult.class,paramMap);
    }
    @DeleteMapping("/api/goods/admin/delGoods")
    public Object delGoods(HttpServletRequest request,Long goodsid) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("goodsid",goodsid);
        String requestURI = request.getRequestURI();
        String url = "http://localhost:8003" + requestURI+"?goodsid={goodsid}";
        System.out.println(url);
        return restTempUtils.delForObject(url, AjaxResult.class,paramMap);
    }
    @GetMapping("/api/goods/admin/updateIsOff")
    public Object updateIsOff(Goods goods, HttpServletRequest request) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("goodsid",goods.getGoodsid());
        paramMap.put("isoff",goods.getIsoff());//admin goods device auth Request Response
        String requestURI = request.getRequestURI();
        String url = "http://localhost:8003" + requestURI+"?goodsid={goodsid}&isoff={isoff}";
        System.out.println(url);
        return restTempUtils.getForObjectByAll(url, AjaxResult.class,paramMap);
    }
    @PostMapping("/api/goods/admin/saveOrUpdateGoods")
    public Object saveOrUpdateGoods(Goods goods, MultipartFile file1) throws IOException {
        //根据商品id 修改商品类型于名称
        if (file1.isEmpty()) {
            return AjaxResult.error("修改商品失败,没有选中文件");
        }
        String filename= UUID.randomUUID().toString()+file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));
        String path = "F:/purl/"+filename;
        String location=path.replace("\\\\", "/");
        //创建文件路径
        File dest = new File(path);
        goods.setGoodspic("uploadImgs/"+filename);
        file1.transferTo(dest);
        System.out.print("保存文件路径"+path+"\n");
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        //param.add("file1", new FileSystemResource(tempFile));
        // param.add("goods",goods);
        param.add("goodsid",goods.getGoodsid());
        param.add("goodscategoryid",goods.getGoodscategoryid());
        param.add("goodsname",goods.getGoodsname());
        param.add("goodsaccpoints",goods.getGoodsaccpoints());
        param.add("goodspic",goods.getGoodspic());
        return restTempUtils.postForObjectByFile("http://localhost:8003/api/goods/admin/saveOrUpdateGoods", param, AjaxResult.class);
    }
    @PostMapping("/api/goods/admin/saveOrAddGoods")
    public Object saveOrAddGoods(Goods goods, MultipartFile file1) throws IOException {
        //添加商品
        if (file1.isEmpty()) {
            return AjaxResult.error("添加商品失败,没有选中文件");
        }
        String filename= UUID.randomUUID().toString()+file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));
        String path = "F:/purl/"+filename;
        String location=path.replace("\\\\", "/");
        //创建文件路径
        File dest = new File(path);
        goods.setGoodspic("uploadImgs/"+filename);
        file1.transferTo(dest);
        System.out.print("保存文件路径"+path+"\n");
        goods.setIsdel(1);
        goods.setIsoff(1);
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        //param.add("file1", new FileSystemResource(tempFile));
       // param.add("goods",goods);
        param.add("goodsid",goods.getGoodsid());
        param.add("goodscategoryid",goods.getGoodscategoryid());
        param.add("goodsname",goods.getGoodsname());
        param.add("goodsaccpoints",goods.getGoodsaccpoints());
        param.add("goodspic",goods.getGoodspic());
        param.add("goodisdel",goods.getIsdel());
        param.add("goodisoff",goods.getIsoff());
        param.add("cname",goods.getCname());
        return restTempUtils.postForObjectByFile("http://localhost:8003/api/goods/admin/saveOrAddGoods", param, AjaxResult.class);
    }
    @PostMapping("/api/goods-category/admin/saveOrUpdateGoodsCategory")
    public Object saveOrUpdateGoodsCategory(GoodsCategory GoodsCategory,HttpServletRequest request) throws IOException {
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        //param.add("file1", new FileSystemResource(tempFile));
        // param.add("goods",goods);
        param.add("goodscategoryid",GoodsCategory.getGoodscategoryid());
        param.add("cname",GoodsCategory.getCname());
        param.add("goodscategoryname",GoodsCategory.getGoodscategoryname());
        return restTempUtils.postForObjectByFile("http://localhost:8003"+request.getRequestURI(), param, AjaxResult.class);
    }
    //http://localhost:8000/api/goods/admin/saveOrUpdateGoods
}
